﻿using CloudDcPc.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc
{
    /// <summary>
    /// OrderYdDlg.xaml 的交互逻辑
    /// </summary>
    public partial class OrderYdDlg : Window
    {
        public OrderYdDlg()
        {
            InitializeComponent();
 
            ObservableCollection<Tab> list_tab = App.TabBean.GetAllTab();
            list_tab_xml.ItemsSource = list_tab;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string book_time = dp_book_time.Text;
            if (book_time == "")
            {
                MessageBox.Show("请输入预订时间！");
                return;
            }
            int size;
            int time_type;
            int tab_num;
            Tab tab = (Tab)list_tab_xml.SelectedItem;
            try
            {
                size = Int32.Parse(txt_size.Text);
                time_type = Int32.Parse(((ComboBoxItem)cb_time_type.SelectedItem).Tag + "");
                tab_num = tab.Tab_num;
            }
            catch
            {
                MessageBox.Show("输入有误！");
                return;
            }
            string note = txt_note.Text;
            System.DateTime cTime = new System.DateTime();
            cTime = System.DateTime.Now;
            string timeStr = cTime.ToString("yyyyMMddHHmmss")+tab_num+"";///////////////                     

            Order order = new Order()
            {
                Order_num = timeStr,
                Book_time = book_time,
                Size = size,
                Time_type = time_type,
                Tab_num = tab_num,
                Note = note,
                User_num = App.user.User_num
            };
            
            if (App.OrderBean.HasYd(order))
            {
                MessageBox.Show("该时间该桌已经被预订！");
                return;
            }

            if (tab.Size < size)
            {
                MessageBox.Show(tab.Name+"最多只能坐"+tab.Size+"人!");
                return;
            }

            if (App.OrderBean.AddYd(order))
            {
                MessageBox.Show("预订成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("预订失败！");
                return;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
